<template>
  <CommonPage v-if="showCommonPage" :params='state.dataForm' @goBack="hideAdvertisement" />
  <div v-if="!showCommonPage">
    <div style="font-size:16px;font-weight: 600;">
      <el-radio></el-radio>人员部门列表
    </div>
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()" style="margin-top: 10px;">
      <el-row>
        <el-form-item>
          <el-button @click="freshDept()">更新列表</el-button>
        </el-form-item>
        <!-- 人数范围 -->
        <el-form-item label="人数范围 ">
          <el-input type="text" placeholder="请输入" style="width: 70px;" v-model="state.dataForm.start"
                    clearable></el-input>
        </el-form-item>
        <el-text style="font-size:16px;margin-top:-30px;padding: 10px 10px;margin-left:-20px;">__</el-text>
        <el-form-item label-width="0px">
          <el-input type="text" placeholder="请输入" style="width: 70px;" v-model="state.dataForm.end"
                    clearable></el-input>
        </el-form-item>
        <!-- 人员数据筐 -->
        <el-form-item>
          <el-checkbox label="人员数据筐" name="type" v-model="state.dataForm.box"></el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button @click="gerinfo()">查询列表</el-button>
        </el-form-item>
        <!--更新方式 -->
        <el-form-item>
          <el-button @click="selectAll()">全选</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="reverseSelect()">反选</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancelSelect()">取消</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query()" :disabled="!isQueryEnabled">查询人员</el-button>
        </el-form-item>
      </el-row>
      <div class="container" v-loading="state.dataForm.loading">
        <el-row v-for="item in groupData" style="width: 20%;">
          <el-col :span="4" class="col-with-padding" v-if="item.count > 0">
            <div class="data-cell">
              <el-tooltip :content="item.deptName" placement="top">
                <el-checkbox v-model="item.isSelected">
                  <div style="display: flex;margin-top: 1px;">
                    <span class="name">{{ item.deptName }}</span>
                    <span class="num">{{ item.count }}</span>
                  </div>
                </el-checkbox>
              </el-tooltip>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import baseService from "@/service/baseService";
import { ElRow, ElCol, ElMessage } from "element-plus";
import { computed, nextTick, onMounted, reactive, ref, toRefs } from 'vue';
import useView from '@/hooks/useView';
import { useRouter } from 'vue-router'
import CommonPage from './information-list-common.vue'; // 引入公共页面组件

const view = reactive({
  dataForm: {
    id: "",
    box: false,
    start: "",
    end: "",
    checkDepts: "",
    loading: false,
  },
});

//进入页面加载
onMounted(() => {
  state.dataForm.loading = false;

});

//是否可以查询
const isQueryEnabled = computed(() => {
  return groupData.value.some(item => item.isSelected);
});

// 定义一个响应式变量，用于控制公共页面的显示和隐藏
const showCommonPage = ref(false);

// 点击查询人员按钮
const query = () => {
  showCommonPage.value = true;
  //处理选中的省份
  for (let i = 0; i < groupData.value.length; i++) {
    if (groupData.value[i].isSelected) {
      state.dataForm.checkDepts += groupData.value[i].deptName + ",";
    }
  }
  //有逗号的话，去掉最后一个逗号
  if (state.dataForm.checkDepts.indexOf(",") != -1) {
    state.dataForm.checkDepts = state.dataForm.checkDepts.substring(0, state.dataForm.checkDepts.length - 1);
  }
};

// 隐藏公共页面
const hideAdvertisement = () => {
  showCommonPage.value = false;
  // 清空选中状态
  groupData.value.forEach(item => {
    item.isSelected = false;
  });
  state.dataForm.checkDepts = "";
};


//初始化
const init = () => {
  gerinfo();
};

//获取城市列表
const groupData = ref<any[]>([]);
const gerinfo = () => {
  state.dataForm.loading = true;
  baseService.get('/crm/crmreportpersondept/list', { type: 1, box: state.dataForm.box, start: state.dataForm.start, end: state.dataForm.end }).then((res) => {
    groupData.value = res.data;
    state.dataForm.loading = false
  });
}

const freshDept = () => {
  baseService.get('/crm/crmreportpersondept/freshDept', {}).then((res) => {
    //提示更新成功
    ElMessage({
      message: '更新成功',
      type: 'success',
    });
    gerinfo();
  });
};


//全选按钮
const selectAll = () => {
  groupData.value.forEach(item => {
    item.isSelected = true;
  });
};

//取消按钮
const cancelSelect = () => {
  groupData.value.forEach(item => {
    item.isSelected = false;
  });
}

//反选按钮
const reverseSelect = () => {
  groupData.value.forEach(item => {
    item.isSelected = !item.isSelected;
  });
};

const state = reactive({ ...useView(view), ...toRefs(view) });

//页面进入执行
init();
</script>

<style scoped>
.container {
  width: 100%;
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
}

.col-with-padding {
  line-height: 40px;
  height: 40px;
  font-size: 14px;
  padding: 0 5px;
}

.num {
  font-size: 12px;
  border-radius: 5px;
  color: #fff;
  padding: 4px 4px 2px 4px;
  background-color: rgba(103, 194, 58, 1);
  margin-top: -3px;
}
.name {
  display: block;
  width: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
